home *** CD-ROM | disk | FTP | other *** search
- Path: anvil.ugrad.cs.ubc.ca!not-for-mail
- From: c2a192@ugrad.cs.ubc.ca (Kazimir Kylheku)
- Newsgroups: comp.lang.c
- Subject: Re: Bitwise Operators, Help with please!
- Date: 12 Feb 1996 11:21:16 -0800
- Organization: Computer Science, University of B.C., Vancouver, B.C., Canada
- Message-ID: <4fo3vcINN7ad@anvil.ugrad.cs.ubc.ca>
- References: <4fjaju$i1o_001@pr.mcs.net>
- NNTP-Posting-Host: anvil.ugrad.cs.ubc.ca
-
- In article <4fjaju$i1o_001@pr.mcs.net>,
- Michael D. Perry <mdp@mika-sys.com> wrote:
- >Hello All;
- >
- >I am trying to teach myself 'C' programming from a book which
- >perhaps is not explaining the Bitwise Operators as well as I
- >would desire in order to understand them.
- >
- >After going through a few examples the book is having me write a
- >program that will pack som data and then unpak using bitwise
- >operators. I have the packing down OK and can print the bits but
- >am just not getting the Unpacking.
- >
- >What I want to do is to unpack each 'field' of a type short with
- >the data as follows:
- >
- >Identification Job Type Gender
- >============== ======== ======
- >bbbbbbbbb bbbbbb b
- >
- >and then be able to read the returned bits and conver them back
- >to their original 'readable' form.
- >
- >Can someone help me with some code so I can at least see how this
- >would be done and maybe understand it then?
- >
- >What I have so far is listed below and thanks for your
- >assistance in advance.
- >
- >Mike
- >
- >
- >= = = = = = = = = =
- >
- >#include <stdio.h>
- >#include <limits.h>
- >
- >short create_employee_data(int, int, char);
- >void bit_print(int);
- >short unpak(int, int);
- >
- >main()
- >{
- > int id_no = 255;
- > int job_type = 63;
- > char gender = 'F';
- > short employee;
- >
- > employee = create_employee_data(id_no,
- > job_type,
- > gender);
- > bit_print(employee);
- > printf("\n%d", char_unpak(employee, 32766));
-
- I don't see char_unpak defined anywhere in your program. There is unpak(),
- which takes a different number of arguments. Thus nobody can help you with the
- claim that your unpacking is not working.
-
- You should probably have a packing/unpacking filter routine for each data type
- that you tend to pack and unpack. That is, pack_employee_data() and
- unpack_employee_data().
-
- By the way, have you heard of bitfields? In a C structure, for integer elements
- you can specify how many bits they are to hold, like this:
-
- struct foo {
- int x : 3; /* x is a "tiny signed integer" of three bits */
- int y : 4; /* y holds four bits */
- }
-
- The field elements may be signed or unsigned. The physical layout depends on
- the machine and the compiler you are using---it may or may not be packed, and
- you can't depend on your code to be portable if you, say, base code on
- the assumption that x and y belong to the same word in a particular order.
-
- > return 0;
- >}
- >
- >short create_employee_data(int id_no,
- > int job_type,
- > char gender)
- >{
- > short employee = 0;
- >
- > employee |= (gender == 'm' || gender == 'M') ? 0 : 1;
-
- This is OK so far!
-
- > employee |= (job_type << 1);
-
- This is bad! job_type could have more bits than you bargained for, thus setting
- too many bits. but let's assume that job_type is already masked by the caller
- to the right number of bits to match the size of the field you are encoding...
-
- > employee |= (id_no << 7);
-
- Ditto.
-
- > return employee;
- >}
- >
- --
-
-